Function Interface হল Java 8-এ যুক্ত একটি ফাংশনাল ইন্টারফেস যা java.util.function প্যাকেজে সংজ্ঞায়িত হয়েছে। এটি একটি Functional Interface, যার মধ্যে শুধুমাত্র একটি অ্যাবস্ট্রাক্ট মেথড থাকে। ফাংশনাল ইন্টারফেসগুলিকে মূলত Lambda Expressions বা Method References দ্বারা ইমপ্লিমেন্ট করা হয়।
Function Interface হল একটি প্রকারের generic functional interface, যা একটি ইনপুট গ্রহণ করে এবং একটি আউটপুট প্রদান করে। এটি UnaryOperator বা BinaryOperator এর মতো অন্যান্য ফাংশনাল ইন্টারফেসের উপর ভিত্তি করে কাজ করে।
Function Interface এর সঠিক সাইনট্যাক্স:
@FunctionalInterface
public interface Function<T, R> {
R apply(T t); // Single abstract method
}
এখানে:
- T হল ইনপুট টাইপ।
- R হল আউটপুট টাইপ।
- apply() মেথড হল ফাংশনাল ইন্টারফেসের একমাত্র অ্যাবস্ট্রাক্ট মেথড, যা ইনপুট গ্রহণ করে এবং আউটপুট প্রদান করে।
Function Interface এর প্রধান বৈশিষ্ট্য
- Single Abstract Method:
- Function Interface এর মধ্যে একমাত্র অ্যাবস্ট্রাক্ট মেথড থাকে, যেটি apply(T t)। এটি একটি ইনপুট T গ্রহণ করে এবং একটি আউটপুট R প্রদান করে।
- Generic Types:
- Function Interface জেনেরিক টাইপ নিয়ে কাজ করে। এটি সাধারণত দুইটি টাইপ প্যারামিটার নেয়: ইনপুট টাইপ T এবং আউটপুট টাইপ R।
- Higher-order Function:
- আপনি Function Interface এর মাধ্যমে ফাংশনাল প্রোগ্রামিং স্টাইল ব্যবহার করে এক ফাংশনকে অন্য ফাংশনের আর্গুমেন্ট বা রিটার্ন ভ্যালু হিসেবে পাস করতে পারেন। এর মাধ্যমে আপনি আরও ফ্লেক্সিবিলিটি এবং মডুলার কোড তৈরি করতে পারেন।
Function Interface এর ব্যবহার
Function Interface ব্যবহার করে আপনি সহজে Lambda Expressions এবং Method References ব্যবহার করে ইনপুট থেকে আউটপুট জেনারেট করতে পারেন।
1. Basic Example Using Function Interface
import java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
// Using Function Interface with Lambda Expression
Function<Integer, Integer> square = x -> x * x;
// Applying the function
System.out.println(square.apply(5)); // Output: 25
}
}
ব্যাখ্যা:
- এখানে Function<Integer, Integer> square একটি Function Interface যা একটি ইনপুট Integer গ্রহণ করে এবং একটি আউটপুট Integer প্রদান করে। আমরা Lambda Expression ব্যবহার করে apply() মেথডটি ইমপ্লিমেন্ট করেছি, যা ইনপুটের স্কয়ার রিটার্ন করবে।
2. Function Interface with Chaining
Function Interface চেইনিংয়ের জন্য অনেক উপকারী, কারণ আপনি একাধিক Function ইন্টারফেসের মধ্যে কার্যক্রম সংযুক্ত করতে পারেন। andThen() এবং compose() মেথডের মাধ্যমে ফাংশন চেইন করা যায়।
- andThen(): এটি একটি ফাংশনের পরে আরেকটি ফাংশন প্রয়োগ করে। প্রথম ফাংশনটির আউটপুট দ্বিতীয় ফাংশনের ইনপুট হিসেবে চলে যায়।
- compose(): এটি একটি ফাংশনের আগে অন্য একটি ফাংশন প্রয়োগ করে। দ্বিতীয় ফাংশনটি প্রথম ফাংশনের ইনপুট হিসেবে চলে যায়।
Example with Chaining Functions:
import java.util.function.Function;
public class FunctionChainingExample {
public static void main(String[] args) {
// Define two functions using Function Interface
Function<Integer, Integer> multiplyBy2 = x -> x * 2;
Function<Integer, Integer> add10 = x -> x + 10;
// Chaining using andThen()
Function<Integer, Integer> multiplyAndThenAdd = multiplyBy2.andThen(add10);
System.out.println(multiplyAndThenAdd.apply(5)); // Output: 20 (5*2 + 10)
// Chaining using compose()
Function<Integer, Integer> addThenMultiply = multiplyBy2.compose(add10);
System.out.println(addThenMultiply.apply(5)); // Output: 30 (5 + 10 * 2)
}
}
ব্যাখ্যা:
multiplyBy2.andThen(add10)প্রথমেmultiplyBy2ফাংশন চালাবে এবং তারপরে তার আউটপুটের উপরadd10ফাংশন প্রয়োগ করবে।multiplyBy2.compose(add10)প্রথমেadd10ফাংশন চালাবে এবং তারপরেmultiplyBy2ফাংশন প্রয়োগ করবে।
Function Interface with Other Functional Interfaces
Java 8 তে অনেক ফাংশনাল ইন্টারফেস রয়েছে যা Function ইন্টারফেসের সাথে মিলে কাজ করতে পারে, যেমন:
- Predicate: এটি একটি Boolean রিটার্ন করে এবং সাধারণত শর্ত যাচাই করতে ব্যবহৃত হয়।
- Consumer: এটি কোনো রিটার্ন ভ্যালু ছাড়া আর্গুমেন্ট গ্রহণ করে।
- Supplier: এটি কোনো ইনপুট ছাড়াই একটি আউটপুট প্রদান করে।
Example with Multiple Functional Interfaces:
import java.util.function.*;
public class FunctionWithOtherInterfaces {
public static void main(String[] args) {
// Function: Takes an Integer, returns its square
Function<Integer, Integer> square = x -> x * x;
// Predicate: Checks if the number is positive
Predicate<Integer> isPositive = x -> x > 0;
// Consumer: Prints the number
Consumer<Integer> print = System.out::println;
// Using Function to calculate square
System.out.println(square.apply(4)); // Output: 16
// Using Predicate to check if number is positive
System.out.println(isPositive.test(4)); // Output: true
System.out.println(isPositive.test(-4)); // Output: false
// Using Consumer to print the number
print.accept(5); // Output: 5
}
}
ব্যাখ্যা:
- এখানে Function একে অপরের সাথে ব্যবহার করা হয়েছে অন্যান্য ফাংশনাল ইন্টারফেসের সাথে, যেমন Predicate (যা Boolean রিটার্ন করে) এবং Consumer (যা কোনো ভ্যালু গ্রহণ করে এবং সেটি প্রিন্ট করে)।
Function Interface এর সুবিধা এবং উপকারিতা:
- Declarative Programming:
- Function Interface ব্যবহার করে আপনি declarative programming স্টাইলে কাজ করতে পারেন। এটি কোডকে পরিষ্কার এবং স্বচ্ছ করে তোলে।
- Code Reusability:
- আপনি যেকোনো ফাংশনকে higher-order function হিসেবে পাস বা রিটার্ন করতে পারেন, যা কোডের পুনঃব্যবহারযোগ্যতা বাড়ায়।
- Chaining:
- andThen() এবং compose() মেথডের মাধ্যমে একাধিক ফাংশন চেইন করা সম্ভব, যা আপনার কাজের মধ্যে আরও ফ্লেক্সিবিলিটি এবং কার্যকারিতা আনে।
- Separation of Concerns:
- বিভিন্ন কাজ বা কার্যাবলী পৃথকভাবে সংজ্ঞায়িত করতে পারলে কোডের মেইনটেন্যান্স এবং রিডেবিলিটি বৃদ্ধি পায়।
সারাংশ:
Function Interface হল Java 8 এর একটি শক্তিশালী ফিচার যা ফাংশনাল প্রোগ্রামিংয়ের ধারণাকে বাস্তবায়িত করে। এটি একটি generic functional interface যা ইনপুট থেকে আউটপুট তৈরির জন্য ব্যবহার হয়। এটি Lambda Expressions এবং Method References এর মাধ্যমে কোডকে আরো সহজ, পরিষ্কার এবং পুনঃব্যবহারযোগ্য করে তোলে। Function Interface ব্যবহারের মাধ্যমে ফাংশনাল প্রোগ্রামিংয়ের সুবিধাগুলি (যেমন declarative style, chaining, higher-order functions) কার্যকরভাবে প্রয়োগ করা যায়।